package com.ruoyi.web.controller.school;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.school.domain.PacAcademicOutput;
import com.ruoyi.school.domain.vo.AcademicCommonVo;
import com.ruoyi.school.domain.vo.PacAcademicOutputVo;
import com.ruoyi.school.service.AcademicOutputService;
import com.ruoyi.system.domain.TableData;
import com.ruoyi.web.controller.tool.WordUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

import static com.ruoyi.web.controller.tool.WordUtil.formatDate;

@Api("学术成果相关接口")
@RestController
@RequestMapping("/school/academicOutput")
public class AcademicOutputController extends BaseController {

    @Resource
    private AcademicOutputService academicOutputService;

    @Resource
    private WordUtil wordUtil;

    @ApiOperation("学术成果列表")
    @GetMapping("/list")
    public TableDataInfo list(PacAcademicOutputVo academicOutput) {
        TableData<PacAcademicOutputVo> list = academicOutputService.getList(academicOutput);
        return getDataTable2(list.getData(), list.getTotal());
    }

    @ApiOperation("学术成果详情")
    @GetMapping("/details")
    public AjaxResult details(PacAcademicOutput pacAcademicOutput) {
        PacAcademicOutput details = academicOutputService.getDetails(pacAcademicOutput);
        return success(details);
    }

    @ApiOperation("学术成果新增")
    @Log(title = "学术成果", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PacAcademicOutput pacAcademicOutput) {
        boolean add = academicOutputService.add(pacAcademicOutput);
        return success(add);
    }

    @ApiOperation("学术成果新增-学生")
    @Log(title = "学术成果", businessType = BusinessType.INSERT)
    @PostMapping("/addByStu")
    public AjaxResult addByStu(@RequestBody PacAcademicOutput pacAcademicOutput) {
        boolean add = academicOutputService.addByStu(pacAcademicOutput);
        return success(add);
    }

    @ApiOperation("学术成果更新")
    @Log(title = "学术成果", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody PacAcademicOutput pacAcademicOutput) {
        boolean update = academicOutputService.edit(pacAcademicOutput);
        return success(update);
    }

    @ApiOperation("学术成果更新-学生")
    @Log(title = "学术成果", businessType = BusinessType.UPDATE)
    @PostMapping("/updateByStu")
    public AjaxResult updateByStu(@RequestBody PacAcademicOutput pacAcademicOutput) {
        boolean update = academicOutputService.editByStu(pacAcademicOutput);
        return success(update);
    }

    @ApiOperation("学术成果删除")
    @Log(title = "学术成果", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult delete(PacAcademicOutput pacAcademicOutput) {
        boolean delete = academicOutputService.delete(pacAcademicOutput);
        return success(delete);
    }

    /**
     * 成果上墙
     */
    @ApiOperation("成果上墙")
    @Log(title = "学术成果(上墙)", businessType = BusinessType.UPDATE)
    @PostMapping("/wall")
    public AjaxResult wall(@RequestBody AcademicCommonVo academicCommonVo) {
        boolean wall = academicOutputService.wall(academicCommonVo);
        return success(wall);
    }

    /**
     * 成果下墙
     */
    @ApiOperation("成果下墙")
    @Log(title = "学术成果(下墙)", businessType = BusinessType.UPDATE)
    @PostMapping("/unWall")
    public AjaxResult unWall(@RequestBody AcademicCommonVo academicCommonVo) {
        boolean wall = academicOutputService.unWall(academicCommonVo);
        return success(wall);
    }


    /**
     * 获取学生的学术成果
     *
     * @param pacAcademicOutput
     * @return
     */
    @ApiOperation("获取学生的学术成果")
    @GetMapping("/myList")
    public TableDataInfo myList(PacAcademicOutput pacAcademicOutput) {
        ArrayList<PacAcademicOutputVo> pacAcademicOutputVos = academicOutputService.myList(pacAcademicOutput);
        return getDataTable(pacAcademicOutputVos);
    }

    @ApiOperation("导出word")
    @PostMapping("/export-word")
    public void exportWord(HttpServletResponse response, PacAcademicOutput pacAcademicOutput) throws IOException {
        // 查询详情
        PacAcademicOutput details = academicOutputService.getDetails(pacAcademicOutput);
        if (details == null) {
            return;
        }
        // 李林在2025年7月8日获得XX大赛三等奖
        // 格式化日期
        String formattedDate = formatDate(details.getCreateTime());
        String titleText = details.getStuName() + "在" + formattedDate + "“" + details.getRecognitionName() + "”比赛中，获得" + details.getAwardLevel();
        WordUtil.SendWord sendWord = new WordUtil.SendWord();
        sendWord.setTitle(titleText);
        if (!Objects.equals(details.getAcFile(), "") && Objects.equals(details.getAcFileType(), "image")) {
            sendWord.setImage(details.getAcFile());
        }
        wordUtil.exportWord(sendWord, response);
    }

    @ApiOperation("导出word")
    @PostMapping("/export-word2")
    public void exportWord2(HttpServletResponse response, PacAcademicOutput pacAcademicOutput) throws IOException {
        List<Long> ids = pacAcademicOutput.getIds();
        if (ids == null || ids.size() == 0) {
            throw new RuntimeException("请选择要导出的数据");
        }
        List<PacAcademicOutput> listByIds = new ArrayList<>();
        // 查询详情
        ids.forEach(item -> {
            pacAcademicOutput.setId(item);
            PacAcademicOutput details = academicOutputService.getDetails(pacAcademicOutput);
            if (details != null) {
                listByIds.add(details);
            }
        });

        if (listByIds.size() == 0) {
            throw new RuntimeException("请选择要导出的数据");
        }

        List<WordUtil.SendWord> sendWordList = new ArrayList<>();
        listByIds.forEach(details -> {
            if (details == null) {
                return;
            }
            // 李林在2025年7月8日获得XX大赛三等奖
            // 格式化日期
            String formattedDate = formatDate(details.getCreateTime());
            String titleText = details.getStuName() + "在" + formattedDate + "“" + details.getRecognitionName() + "”比赛中，获得" + details.getAwardLevel();
            WordUtil.SendWord sendWord = new WordUtil.SendWord();
            sendWord.setTitle(titleText);
            if (!Objects.equals(details.getAcFile(), "") && Objects.equals(details.getAcFileType(), "image")) {
                sendWord.setImage(details.getAcFile());
            }
            sendWordList.add(sendWord);
        });
        wordUtil.exportListWord(sendWordList, response);
    }

    // 批量导出
    @PostMapping("/export-word-batch")
    public void exportWordBatch(HttpServletResponse response, @RequestBody List<Long> ids) throws IOException {
        List<PacAcademicOutput> pacAcademicOutputs = academicOutputService.getListByIds(ids);
        for (PacAcademicOutput details : pacAcademicOutputs) {
            // 李林在2025年7月8日获得XX大赛三等奖
            // 格式化日期
            String formattedDate = formatDate(details.getCreateTime());
            String titleText = details.getStuName() + "在" + formattedDate + "“" + details.getRecognitionName() + "”比赛中，获得" + details.getAwardLevel();
            WordUtil.SendWord sendWord = new WordUtil.SendWord();
            sendWord.setTitle(titleText);
            if (!Objects.equals(details.getAcFile(), "") && Objects.equals(details.getAcFileType(), "image")) {
                sendWord.setImage(details.getAcFile());
            }
            wordUtil.exportWord(sendWord, response);
        }
    }

}
